package com.raidpoints.client;

import java.sql.Date;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.LayoutPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.datepicker.client.DatePicker;
import com.google.gwt.user.datepicker.client.DateBox;
import com.google.gwt.user.client.ui.ValuePicker;
import com.google.gwt.user.cellview.client.CellList;
import com.google.gwt.cell.client.TextCell;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.datepicker.client.DateBox.DefaultFormat;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.i18n.client.TimeZone;
import com.google.gwt.user.client.ui.TextBox;
import com.raidpoints.client.RaidPoints.HasRedraw;
import com.summatech.gwt.client.HourMinutePicker;
import com.summatech.gwt.client.HourMinutePicker.PickerFormat;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.KeyDownEvent;
import com.google.gwt.event.dom.client.KeyDownHandler;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.IntegerBox;
import com.google.gwt.user.client.ui.CheckBox;

public class NewEventBoxOther extends DialogBox {
	RaidPoints rp;
	ListBox gameBox;

	TextArea noteBox;
	event evt;
	CheckBox chckbxCancel;
	TextBox startBox; 
	TextBox endBox;
	Button btnAdd;
	DateBox dateBox;
	PointsPage pp;
	ArrayList<gameinfo> games;
	TextBox destBox;
	CheckBox autoslotBox;
	
	public NewEventBoxOther(RaidPoints mrp){
		this(mrp,null,null,null);
	}
	
	public NewEventBoxOther(RaidPoints mrp, event mevt){
		this(mrp,null,null,mevt);
	}
	
	public NewEventBoxOther(RaidPoints mrp, java.util.Date dt, event mevt){
		this(mrp,null,dt,mevt);
	}


	/**
	 * @wbp.parser.constructor
	 */
	public NewEventBoxOther(RaidPoints mrp, PointsPage mpp, java.util.Date dt, event mevt) {
		this.rp = mrp;
		evt = mevt;
		pp = mpp;
		games = new ArrayList<gameinfo>();
		if(evt == null) setHTML("Add Event");
		else setHTML("Modify Event");
		
		LayoutPanel layoutPanel = new LayoutPanel();
		setWidget(layoutPanel);
		layoutPanel.setSize("333px", "411px");
		
		Label lblNewLabel = new Label("Destination (e.g. Scarlet Monestary):");
		layoutPanel.add(lblNewLabel);
		layoutPanel.setWidgetLeftWidth(lblNewLabel, 22.0, Unit.PX, 252.0, Unit.PX);
		layoutPanel.setWidgetTopHeight(lblNewLabel, 69.0, Unit.PX, 16.0, Unit.PX);
		
		dateBox = new DateBox();
		dateBox.setFormat(new DefaultFormat(DateTimeFormat.getFormat("y MMM d")));
		layoutPanel.add(dateBox);
		layoutPanel.setWidgetLeftWidth(dateBox, 125.0, Unit.PX, 157.0, Unit.PX);
		layoutPanel.setWidgetTopHeight(dateBox, 147.0, Unit.PX, 32.0, Unit.PX);
		if(dt != null) dateBox.setValue(dt);
		
		
		Label lblNewLabel_1 = new Label("Date:");
		layoutPanel.add(lblNewLabel_1);
		layoutPanel.setWidgetLeftWidth(lblNewLabel_1, 22.0, Unit.PX, 56.0, Unit.PX);
		layoutPanel.setWidgetTopHeight(lblNewLabel_1, 163.0, Unit.PX, 16.0, Unit.PX);
		
		Label lblStart = new Label("Start:");
		layoutPanel.add(lblStart);
		layoutPanel.setWidgetLeftWidth(lblStart, 22.0, Unit.PX, 56.0, Unit.PX);
		layoutPanel.setWidgetTopHeight(lblStart, 202.0, Unit.PX, 16.0, Unit.PX);
		
		startBox = new TextBox();
		startBox.setMaxLength(5);
		startBox.setText("12:00");
		startBox.setWidth("55px");
		layoutPanel.add(startBox);
		layoutPanel.setWidgetLeftWidth(startBox, 66.0, Unit.PX, 65.0, Unit.PX);
		layoutPanel.setWidgetTopHeight(startBox, 186.0, Unit.PX, 32.0, Unit.PX);
		
		Label lblEnd = new Label("End:");
		layoutPanel.add(lblEnd);
		layoutPanel.setWidgetLeftWidth(lblEnd, 191.0, Unit.PX, 56.0, Unit.PX);
		layoutPanel.setWidgetTopHeight(lblEnd, 202.0, Unit.PX, 16.0, Unit.PX);
		
		endBox = new TextBox();
		endBox.setMaxLength(5);
		endBox.setText("13:00");
		layoutPanel.add(endBox);
		endBox.setWidth("55px");
		layoutPanel.setWidgetLeftWidth(endBox, 233.0, Unit.PX, 65.0, Unit.PX);
		layoutPanel.setWidgetTopHeight(endBox, 185.0, Unit.PX, 32.0, Unit.PX);
		
		Label lblPointsPool = new Label("Game:");
		layoutPanel.add(lblPointsPool);
		layoutPanel.setWidgetLeftWidth(lblPointsPool, 22.0, Unit.PX, 149.0, Unit.PX);
		layoutPanel.setWidgetTopHeight(lblPointsPool, 17.0, Unit.PX, 16.0, Unit.PX);
		
		btnAdd = new Button("Add");
		btnAdd.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				if(!timevalid(startBox.getText()) || !timevalid(endBox.getText())){
					Window.alert("Start and/or end time invald. Format: hh:mm (24-hour)");
					return;
				}
				if(dateBox.getTextBox().getText().equals("")){
					Window.alert("Must enter date");
					return;
				}
			
				if(evt == null){
					AsyncCallback<ArrayList<event>> addEventCallback = new AsyncCallback<ArrayList<event>>(){

						@Override
						public void onFailure(Throwable caught) {
							// TODO Auto-generated method stub
							
						}

						@Override
						public void onSuccess(ArrayList<com.raidpoints.client.event> result) {
							if(result==null) return;
							rp.dataProvider.getList().addAll(result);
							Collections.sort(rp.dataProvider.getList(),rp.tsComp);
							Collections.reverse(rp.dataProvider.getList());		
							for(event e: result){
							((RaidPoints.HasRedraw) rp.navmap.get(rp.eventItem)).AddEvent(e);
							}
						}
						
					};
				event te = new event();
				te.dest = destBox.getText();
				te.secdest = "nodest";
				te.game = games.get(gameBox.getSelectedIndex()).game;
				te.gid = games.get(gameBox.getSelectedIndex()).ngsid;
				te.note = noteBox.getText();
				te.visible = (autoslotBox.getValue() ? 1 : 0);
				java.util.Date dt = dateBox.getValue();
				int sh = Integer.parseInt(startBox.getText().split(":")[0]);
				int sm = Integer.parseInt(startBox.getText().split(":")[1]);
				int eh = Integer.parseInt(endBox.getText().split(":")[0]);
				int em = Integer.parseInt(endBox.getText().split(":")[1]);
				
				te.starttime = new NCDate(dt.getYear() + 1900, dt.getMonth() + 1, dt.getDate(), sh, sm);
				te.endtime = new NCDate(dt.getYear() + 1900, dt.getMonth() + 1, dt.getDate(), eh, em);

		//		te.starttime = new Timestamp(dt.getYear(),dt.getMonth(),dt.getDate(),sh,sm,0,0);
		//		te.endtime = new Timestamp(dt.getYear(),dt.getMonth(),dt.getDate(),eh,em,0,0);
				rp.rpsvc.AddEvent(te, 1,addEventCallback);
				}
				else{
					AsyncCallback<event> updateCallback = new AsyncCallback<event>(){

						@Override
						public void onFailure(Throwable caught) {
							// TODO Auto-generated method stub
							
						}

						@Override
						public void onSuccess(com.raidpoints.client.event result) {
								for(event e : rp.dataProvider.getList()){
									if(e.eid == evt.eid){
										for(int i = 0; i < rp.eventItem.getChildCount(); i++){
											if(((PointsPage)rp.navmap.get(rp.eventItem.getChild(i))).evt.eid == evt.eid){
												if(result == null) rp.RemoveTab(rp.eventItem.getChild(i));
												else ((PointsPage)rp.navmap.get(rp.eventItem.getChild(i))).Refresh();
												break;
											}
										}
										boolean rc  = false;
										if(rp.navmap.get(rp.eventItem) instanceof RaidCalendar){
											rc = true;
											((RaidCalendar) rp.navmap.get(rp.eventItem)).RemoveEvent(evt);
										}	
										rp.dataProvider.getList().remove(e);
										if(result != null){
											rp.dataProvider.getList().add(result);
											if(rc) ((RaidCalendar) rp.navmap.get(rp.eventItem)).AddEvent(evt);
										}	
										((HasRedraw) rp.navmap.get(rp.eventItem)).Redraw();
										break;
									}
								}

							hide();
						}
						
					};
					evt.dest  = destBox.getText();
					evt.game = games.get(gameBox.getSelectedIndex()).game;
					evt.note = noteBox.getText();
					evt.gid = games.get(gameBox.getSelectedIndex()).ngsid;
					evt.visible = (autoslotBox.getValue() ? 1 : 0);
					java.util.Date dt = dateBox.getValue();
					int sh = Integer.parseInt(startBox.getText().split(":")[0]);
					int sm = Integer.parseInt(startBox.getText().split(":")[1]);
					int eh = Integer.parseInt(endBox.getText().split(":")[0]);
					int em = Integer.parseInt(endBox.getText().split(":")[1]);
					evt.starttime = new NCDate(dt.getYear() + 1900, dt.getMonth() + 1, dt.getDate(), sh, sm);
					evt.endtime = new NCDate(dt.getYear() + 1900, dt.getMonth() + 1, dt.getDate(), eh, em);
					rp.rpsvc.UpdateEvent(evt, chckbxCancel.getValue(), updateCallback);
				}
				hide();
			}
		});
		layoutPanel.add(btnAdd);
		layoutPanel.setWidgetLeftWidth(btnAdd, 50.0, Unit.PX, 81.0, Unit.PX);
		layoutPanel.setWidgetTopHeight(btnAdd, 372.0, Unit.PX, 28.0, Unit.PX);
		
		Button btnCancel = new Button("Cancel");
		btnCancel.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				hide();
			}
		});
		layoutPanel.add(btnCancel);
		layoutPanel.setWidgetLeftWidth(btnCancel, 166.0, Unit.PX, 81.0, Unit.PX);
		layoutPanel.setWidgetTopHeight(btnCancel, 372.0, Unit.PX, 28.0, Unit.PX);
		
		gameBox = new ListBox();
		gameBox.setWidth("276px");
		layoutPanel.add(gameBox);
		layoutPanel.setWidgetLeftWidth(gameBox, 22.0, Unit.PX, 300.0, Unit.PX);
		layoutPanel.setWidgetTopHeight(gameBox, 39.0, Unit.PX, 24.0, Unit.PX);
		
		Label lblNote = new Label("Note:");
		layoutPanel.add(lblNote);
		layoutPanel.setWidgetLeftWidth(lblNote, 22.0, Unit.PX, 56.0, Unit.PX);
		layoutPanel.setWidgetTopHeight(lblNote, 275.0, Unit.PX, 16.0, Unit.PX);
		
		noteBox = new TextArea();
		noteBox.setWidth("232px");
		layoutPanel.add(noteBox);
		layoutPanel.setWidgetLeftWidth(noteBox, 66.0, Unit.PX, 245.0, Unit.PX);
		layoutPanel.setWidgetTopHeight(noteBox, 239.0, Unit.PX, 73.0, Unit.PX);
		
		chckbxCancel = new CheckBox("Cancel");
		layoutPanel.add(chckbxCancel);
		layoutPanel.setWidgetLeftWidth(chckbxCancel, 50.0, Unit.PX, 109.0, Unit.PX);
		layoutPanel.setWidgetTopHeight(chckbxCancel, 347.0, Unit.PX, 19.0, Unit.PX);
		
		destBox = new TextBox();
		layoutPanel.add(destBox);
		layoutPanel.setWidgetLeftWidth(destBox, 22.0, Unit.PX, 276.0, Unit.PX);
		layoutPanel.setWidgetTopHeight(destBox, 96.0, Unit.PX, 32.0, Unit.PX);
		
		autoslotBox = new CheckBox("Auto-slot new signups");
		layoutPanel.add(autoslotBox);
		layoutPanel.setWidgetLeftWidth(autoslotBox, 50.0, Unit.PX, 242.0, Unit.PX);
		layoutPanel.setWidgetTopHeight(autoslotBox, 318.0, Unit.PX, 19.0, Unit.PX);
	
		chckbxCancel.setVisible(evt != null);
		
		gameBox.addChangeHandler(new ChangeHandler(){

			@Override
			public void onChange(ChangeEvent event) {
				if(rp.destlist == null) return;
				String game = games.get(gameBox.getSelectedIndex()).game;
				destBox.setText(""); 
			}
			
		});	
		
		AsyncCallback<ArrayList<gameinfo>> gameCallback = new AsyncCallback<ArrayList<gameinfo>>(){

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onSuccess(ArrayList<gameinfo> result) {
				games.clear();
				games.addAll(result);
				gameBox.clear();
				for(int i = 0; i < games.size(); i++) gameBox.addItem(games.get(i).game);
				if(evt != null) popExist();
			}
			
		};
		rp.rpsvc.GetGames(gameCallback);				
	}
	
	public void popExist(){
		for(int i = 0; i < gameBox.getItemCount(); i++){
			if(gameBox.getItemText(i).equals(evt.game)){
				gameBox.setSelectedIndex(i);
				break;
			}
		}
		destBox.setText(evt.dest);
		startBox.setText(evt.starttime.getTime()); endBox.setText(evt.endtime.getTime());
		noteBox.setText(evt.note);
		java.util.Date dtt = new java.util.Date();
		dtt.setMonth(evt.starttime.month - 1);
		dtt.setYear(evt.starttime.year);
		dtt.setDate(evt.starttime.day);
		dateBox.setValue(dtt);
		btnAdd.setText("Update");
		autoslotBox.setValue(evt.visible == 1);
	}
	
	public boolean timevalid(String time){
		if(time.matches("[0-2]{0,1}[0-9]{0,1}[:]{0,1}[0-5]{0,1}[0-9]{0,1}")) return true;
		else return false;
	}
}
